<?php

namespace app\store\service;


use app\store\model\Order;
use think\facade\Db;

class OrderService
{
    /**
     * 全部订单
     * */
    public static function getOrderList($param)
    {

        $store_id = session('store_id');


        $query = Order::alias('a')
                    ->field('a.id,a.order_no,a.discount_amount,a.real_amount,a.status,a.create_time,b.nickname,b.avatar,c.trade_method')
                    ->with('items')
                    ->join('user b','a.user_id=b.user_id')
                    ->join('order_trade c','a.id=c.order_id')
                    ->where('a.store_id',$store_id)
                    ->withAttr('trade_method',function ($value){
                        $type = [1 => '微信支付',2 => '支付宝支付',3 =>'余额支付', 4 => '积分兑换',5 => '免费拿'];
                        return $type[$value];
                    });

        if (!empty($param['start_time']) && !empty($param['end_time'])){
            $query->whereBetweenTime('a.create_time',$param['start_time'],$param['end_time']);
        }

       if (!empty($param['keyword'])){
           $query->whereOr('b.nickname',$param['keyword'])->where('a.order_no',$param['keyword']);
       }

       $orders = $query->order('a.id','desc')
                       ->paginate(10);


       return $orders;
    }


    /**
     * 待发货
     * */
    public static function getDeliveryList($param)
    {
        $store_id = session('store_id');

        $query = Order::alias('a')
            ->field('a.id,a.order_no,a.discount_amount,a.real_amount,a.status,a.create_time,b.nickname,b.avatar')
            ->with('items')
            ->join('user b','a.user_id=b.user_id')
            ->where('a.store_id',$store_id)
            ->where('a.status',2);

        if (!empty($param['start_time']) && !empty($param['end_time'])){
            $query->whereBetweenTime('a.create_time',$param['start_time'],$param['end_time']);
        }

        if (!empty($param['keyword'])){
            $query->whereOr('b.nickname',$param['keyword'])->where('a.order_no',$param['keyword']);
        }

        $orders = $query->order('a.id','desc')->paginate(10);

        return $orders;
    }
    /**
     * 待收货
     * */
    public static function getReceiptList($param)
    {
        $store_id = session('store_id');

        $query = Order::alias('a')
            ->field('a.id,a.order_no,a.discount_amount,a.real_amount,a.status,a.create_time,b.nickname,b.avatar')
            ->with('items')
            ->join('user b','a.user_id=b.user_id')
            ->where('a.store_id',$store_id)
            ->where('a.status',3);

        if (!empty($param['start_time']) && !empty($param['end_time'])){
            $query->whereBetweenTime('a.create_time',$param['start_time'],$param['end_time']);
        }

        if (!empty($param['keyword'])){
            $query->whereOr('b.nickname',$param['keyword'])->where('a.order_no',$param['keyword']);
        }

        $orders = $query->order('a.id','desc')->paginate(10);

        return $orders;
    }
    /**
     * 待付款
     * */
    public static function getPayList($param)
    {
        $store_id = session('store_id');

        $query = Order::alias('a')
            ->field('a.id,a.order_no,a.discount_amount,a.real_amount,a.status,a.create_time,b.nickname,b.avatar')
            ->with('items')
            ->join('user b','a.user_id=b.user_id')
            ->where('a.store_id',$store_id)
            ->where('a.status',1);

        if (!empty($param['start_time']) && !empty($param['end_time'])){
            $query->whereBetweenTime('a.create_time',$param['start_time'],$param['end_time']);
        }

        if (!empty($param['keyword'])){
            $query->whereOr('b.nickname',$param['keyword'])->where('a.order_no',$param['keyword']);
        }

        $orders = $query->order('a.id','desc')->paginate(10);

        return $orders;
    }

    /**
     * 已完成
     * */
    public static function getFinishList($param)
    {
        $store_id = session('store_id');

        $query = Order::alias('a')
            ->field('a.id,a.order_no,a.discount_amount,a.real_amount,a.status,a.create_time,b.nickname,b.avatar')
            ->with('items')
            ->join('user b','a.user_id=b.user_id')
            ->where('a.store_id',$store_id)
            ->where('a.status',4);

        if (!empty($param['start_time']) && !empty($param['end_time'])){
            $query->whereBetweenTime('a.create_time',$param['start_time'],$param['end_time']);
        }

        if (!empty($param['keyword'])){
            $query->whereOr('b.nickname',$param['keyword'])->where('a.order_no',$param['keyword']);
        }

        $orders = $query->order('a.id','desc')->paginate(10);

        return $orders;
    }


    /**
     * 已取消
     * */
    public static function getCancelList($param)
    {
        $store_id = session('store_id');

        $query = Order::alias('a')
            ->field('a.id,a.order_no,a.discount_amount,a.real_amount,a.status,a.create_time,b.nickname,b.avatar')
            ->with('items')
            ->join('user b','a.user_id=b.user_id')
            ->where('a.store_id',$store_id)
            ->where('a.status',-1);

        if (!empty($param['start_time']) && !empty($param['end_time'])){
            $query->whereBetweenTime('a.create_time',$param['start_time'],$param['end_time']);
        }

        if (!empty($param['keyword'])){
            $query->whereOr('b.nickname',$param['keyword'])->where('a.order_no',$param['keyword']);
        }

        $orders = $query->order('a.id','desc')->paginate(10);

        return $orders;
    }


    /**
     * 售后订单
     * */
    public static function getAfterSaleList($param)
    {
        $store_id = session('store_id');

        $query = Order::alias('a')
            ->field('a.id,a.order_no,a.discount_amount,a.real_amount,a.status,a.create_time,b.nickname,b.avatar')
            ->with('items')
            ->join('user b','a.user_id=b.user_id')
            ->where('a.store_id',$store_id)
            ->where('a.is_after_sale',1);

        if (!empty($param['start_time']) && !empty($param['end_time'])){
            $query->whereBetweenTime('a.create_time',$param['start_time'],$param['end_time']);
        }

        if (!empty($param['keyword'])){
            $query->whereOr('b.nickname',$param['keyword'])->where('a.order_no',$param['keyword']);
        }

        $orders = $query->order('a.id','desc')->paginate(10);
        return $orders;
    }


    public static function findOrder($order_id)
    {
        $order = Db::name('order')->alias('a')
                        ->field('a.id,a.order_no,a.discount_amount,a.real_amount,b.receiver_phone,b.receiver_name,
                        b.province,b.city,b.county,b.address,a.status,a.create_time,c.nickname,c.avatar,d.trade_method,d.out_trade_no,
                        d.trade_time,d.trade_status,d.trade_fee')
                        ->join('order_address b','a.id=b.order_id', 'left')
                        ->join('user c','a.user_id=c.user_id', 'left')
                        ->join('order_trade d','a.id=d.order_id', 'left')
                        ->where('a.id',$order_id)
                        ->withAttr('trade_method',function ($value){
                            $type = [1 => '微信支付',2 => '支付宝支付',3 =>'余额支付', 4 => '积分兑换',5 => '免费拿'];
                            return $type[$value];
                        })->withAttr('trade_status',function ($value){
                            $type = [-1 => '交易失败',1 => '交易成功'];
                            return $type[$value];
                        })->withAttr('status',function ($value){
                            $status = [-1 => '已取消',1 => '代付款', 2 => '待发货',3 => '待收货', 4=> '已完成'];
                            return $status[$value];
                        })->find();
        $order['items'] = Db::name('order_item')->where('order_id',$order['id'])->select();

        return $order;
    }

    /**
     * 退款订单查询
     * */
    public static function findRefundOrder($order_id)
    {
        $order = Db::name('order')
            ->field('id,out_trade_no,store_id,payment_type,real_amount')
            ->where('id',$order_id)
            ->find();

        return $order;
    }


}